package io.iec.edp.caf.msu.provider;

import io.iec.edp.caf.common.JSONSerializer;
import io.iec.edp.caf.commons.runtime.CafEnvironment;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.msu.api.ServiceUnitAwareService;
import io.iec.edp.caf.msu.api.client.ServiceDiscovery;
import io.iec.edp.caf.msu.api.client.ServiceRegistry;
import io.iec.edp.caf.msu.api.entity.MsuProperties;
import io.iec.edp.caf.msu.api.entity.ServiceUnitInfo;
import io.iec.edp.caf.msu.api.entity.ServiceUnitRegisterInfo;
import io.iec.edp.caf.rpc.api.entity.ServiceUnitDefinition;
import io.iec.edp.caf.rpc.api.spi.ServiceCenterProvider;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * MSU实现RPC ServiceCenterProvider
 */
@Slf4j
public class MsuServiceCenterProvider implements ServiceCenterProvider {

    //服务发现
    private ServiceDiscovery serviceDiscovery;
    //服务注册
    private ServiceRegistry serviceRegistry;
    //msu config
    private MsuProperties configuration;

    public MsuServiceCenterProvider() {
    }

//    public MsuServiceCenterProvider(ServiceDiscovery serviceDiscovery, ServiceRegistry serviceRegistry, MsuProperties configuration) {
//        this.serviceDiscovery = serviceDiscovery;
//        this.serviceRegistry = serviceRegistry;
//        this.configuration = configuration;
//    }

    public String discover(ServiceUnitDefinition serviceUnitDefinition) {
        if (this.serviceDiscovery == null) this.serviceDiscovery = SpringBeanUtils.getBean(ServiceDiscovery.class);

        return this.serviceDiscovery.discover(serviceUnitDefinition.getMsu(), serviceUnitDefinition.getEventContext());
    }

    @Override
    public void registry(ServiceUnitDefinition serviceUnitDefinition) {
        if (this.configuration == null) this.configuration = SpringBeanUtils.getBean(MsuProperties.class);
        if (this.serviceRegistry == null) this.serviceRegistry = SpringBeanUtils.getBean(ServiceRegistry.class);

        String ip = CafEnvironment.getLocalIp(this.configuration.getIp().getPrefix());
        //组装实例注册信息
        //获取当前应用服务器下的所有SU信息
        ServiceUnitAwareService suAware = SpringBeanUtils.getBean(ServiceUnitAwareService.class);
        ServiceUnitRegisterInfo serviceUnitRegisterInfo = suAware.getServiceUnitRegisterInfo(this.configuration.getApplicationName(), this.configuration.getServiceName());
        //克隆一下
        ServiceUnitRegisterInfo grpcRegisterInfo = JSONSerializer.deserialize(JSONSerializer.serialize(serviceUnitRegisterInfo), ServiceUnitRegisterInfo.class);
        if (serviceUnitDefinition.getMsuSuffix() != null && serviceUnitDefinition.getMsuSuffix().length() > 0) {
            List<ServiceUnitInfo> serviceUnitInfos = grpcRegisterInfo.getServiceUnitInfo();
            serviceUnitInfos.forEach(info -> info.setName(info.getName() + serviceUnitDefinition.getMsuSuffix()));

        }

        //注册微服务信息
        this.serviceRegistry.register(grpcRegisterInfo, ip, serviceUnitDefinition.getPort());
    }

    @Override
    public void registry() {
        if (this.configuration == null) this.configuration = SpringBeanUtils.getBean(MsuProperties.class);
        if (this.serviceRegistry == null) this.serviceRegistry = SpringBeanUtils.getBean(ServiceRegistry.class);

        //组装实例注册信息
        //获取当前应用服务器下的所有SU信息
        ServiceUnitAwareService suAware = SpringBeanUtils.getBean(ServiceUnitAwareService.class);
        ServiceUnitRegisterInfo registerInfo = suAware.getServiceUnitRegisterInfo(this.configuration.getApplicationName(), this.configuration.getServiceName());

        log.info("ServiceCenter type [{}]", this.serviceRegistry.getClass().getName());
        //注册微服务信息
        this.serviceRegistry.register(registerInfo);
    }
}
